package com.rh.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rh.entity.EduSubject;
import com.rh.entity.excel.SubjectData;
import com.rh.entity.excel.SubjectExportVo;
import com.rh.entity.subject.OneSubject;
import com.rh.entity.subject.TwoSubject;
import com.rh.listener.SubjectExcelListener;
import com.rh.mapper.EduSubjectMapper;
import com.rh.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.exception.EducationException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            //文件输入流
            InputStream in=file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new EducationException(20002,"添加课程分类失败");
        }
    }
    //课程列表树形结构
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
       //先查询1级分类,parent_id为0是1级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> eduSubjectsOne = baseMapper.selectList(wrapperOne);
        // 再查询2级分类,parent_id不为0是2级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> eduSubjectsTwo = baseMapper.selectList(wrapperTwo);
        //创建一个集合存放最终树形结构
        List<OneSubject>finalSubjectAll=new ArrayList<>();
        //循环查询出来的1级分类列表
        for (int i = 0; i < eduSubjectsOne.size(); i++) {
            EduSubject eduSubjectOne = eduSubjectsOne.get(i);
            OneSubject oneSubject = new OneSubject();
            //把eduSubjectOne中的值复制到oneSubject中
            BeanUtils.copyProperties(eduSubjectOne,oneSubject);
            //创建一个集合用于存放2级分类的数据
            List<TwoSubject>finalSubjectTwo=new ArrayList<>();
            //在1级分类中循环2级分类,并设置值
            for (int j = 0; j < eduSubjectsTwo.size(); j++) {
                EduSubject eduSubjectTwo = eduSubjectsTwo.get(j);
                //这里判断如果2级分类的parent_id值和1级分类的id值相等,说明是新分类 就加入到1级分类中
                if(eduSubjectTwo.getParentId().equals(eduSubjectOne.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    //把eduSubjectTwo对应的值复制到twoSubject中
                    BeanUtils.copyProperties(eduSubjectTwo,twoSubject);
                    finalSubjectTwo.add(twoSubject);
                }
            }
            //因为1级分类包含2级分类，将2级分类最终集合的值添加到1级分类对象中
            oneSubject.setChildren(finalSubjectTwo);
            //把1级分类对象添加到最终分类集合中
            finalSubjectAll.add(oneSubject);
        }
        return finalSubjectAll;
    }

    /**
     * 导出课程分类
     */
    @Override
    public void exportSubject(HttpServletResponse response) {
        //设置文件类型excel
        response.setContentType("application/vnd.ms-excel");
        //文件编码
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String fileName ="Subject";
        //设置响应头信息
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        //查询所有列表信息
        List<EduSubject>subjectList=baseMapper.selectList(null);
        //把EduSubject转换成SubjectExportVo
        //先创建一个集合来存放最终数据
        List<SubjectExportVo>finalSubjectExportVo=new ArrayList<>();
        //循环subjectList复制到SubjectExportVo中
        for (EduSubject eduSubject : subjectList) {
            SubjectExportVo subjectExportVo = new SubjectExportVo();
            //把EduSubject中的值复制到Vo中
            BeanUtils.copyProperties(eduSubject,subjectExportVo);
            finalSubjectExportVo.add(subjectExportVo);
        }
        try{
            //调用方法执行写的操作
          EasyExcel.write(response.getOutputStream(),SubjectExportVo.class).sheet("Subject").doWrite(finalSubjectExportVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new EducationException(30002,"导出课程列表失败");
        }
    }
}
